/*** server ***/
package db;

import java.util.ArrayList;
import util.Utils;

public class DataBase {

    /**************************************************************************************************************
     * tem que chamar a communication para requisitar do banco de dados global caso esteja conectado
     **************************************************************************************************************/
    public static final int PRODUCTS = 1;
    public static final int SALES = 2;
    public static final int REQUISITIONS = 3;
    private static boolean set = false;
    private static ArrayList<String[]> products = new ArrayList<String[]>();
    private static ArrayList<String[]> sales = new ArrayList<String[]>();
    private static ArrayList<String[]> req = new ArrayList<String[]>();

    public static void init() {

        products = new ArrayList<String[]>();
        //"ID", "Nome", "Tipo", "Preço", "Quantia em estoque", "inf", "sup"
        products.add(new String[]{"4", "Cotrimoxazol", "Comprimidos", "R$ 05.95", "42", "42", "42"});
        products.add(new String[]{"8", "Eritromicina", "Comprimidos", "R$ 07.98", "43", "43", "43"});
        products.add(new String[]{"15", "Metronidazol", "Comprimidos", "R$ 09.90", "44", "44", "44"});
    }

    public static boolean addVal(String id, int quantity) {
        String[] query = readData(0, id, PRODUCTS);
        int initialValue = Integer.valueOf(query[4]);
        int finalValue;
        finalValue = initialValue + quantity;
        query[4] = String.valueOf(finalValue);
        return writeData(PRODUCTS, query);
    }

    public static boolean subVal(String id, int quantity) {
        String[] query = readData(0, id, PRODUCTS);
        int initialValue = Integer.valueOf(query[4]);
        int finalValue;
        if (initialValue >= quantity) {
            finalValue = initialValue - quantity;
            query[4] = String.valueOf(finalValue);
            return writeData(PRODUCTS, query);
        } else {
            return false;
        }
    }

    public static boolean addInf(String id, int quantity) {
        String[] query = readData(0, id, PRODUCTS);
        int initialValue = Integer.valueOf(query[5]);
        int finalValue;
        finalValue = initialValue + quantity;
        query[5] = String.valueOf(finalValue);
        return writeData(PRODUCTS, query);
    }

    public static boolean subInf(String id, int quantity) {
        String[] query = readData(0, id, PRODUCTS);
        int initialValue = Integer.valueOf(query[5]);
        int finalValue;
        if (initialValue >= quantity) {
            finalValue = initialValue - quantity;
            query[5] = String.valueOf(finalValue);
            return writeData(PRODUCTS, query);
        } else {
            return false;
        }
    }

    public static boolean addSup(String id, int quantity) {
        String[] query = readData(0, id, PRODUCTS);
        int initialValue = Integer.valueOf(query[6]);
        int finalValue;
        finalValue = initialValue + quantity;
        query[6] = String.valueOf(finalValue);
        return writeData(PRODUCTS, query);
    }

    public static boolean subSup(String id, int quantity) {
        String[] query = readData(0, id, PRODUCTS);
        int initialValue = Integer.valueOf(query[6]);
        int finalValue;
        if (initialValue >= quantity) {
            finalValue = initialValue - quantity;
            query[6] = String.valueOf(finalValue);
            return writeData(PRODUCTS, query);
        } else {
            return false;
        }
    }

    private static ArrayList getList(int type) {
        if (!set) {
            init();
        }
        ArrayList list;
        switch (type) {
            case PRODUCTS:
                list = products;
                break;
            case SALES:
                list = sales;
                break;
            case REQUISITIONS:
                list = req;
                break;
            default:
                list = products;
                break;
        }
        return list;
    }

    public static String[][] readData(int queryType) {
        return Utils.listToArray(getList(queryType));
    }

    public static String[] readData(int column, String q, int queryType) {
        ArrayList list = getList(queryType);
        if (column < 0 || column > list.size() - 1) {
            return null;
        }
        for (int i = 0; i < list.size(); i++) {
            String[] s = (String[]) list.get(i);
            if (s[column].equals(q)) {
                return s;
            }
        }
        return null;
    }

    public static boolean writeData(int queryType, String[] query) {
        return write(getList(queryType), query);
    }

    public static boolean deleteData(int queryType, String[] query) {
        return delete(getList(queryType), query);
    }

    public static boolean deleteData(int column, String q, int queryType) {
        String[] s = readData(column, q, queryType);
        if (s == null) {
            return false;
        }
        return deleteData(queryType, s);
    }

    private static boolean write(ArrayList<String[]> list, String[] q) {
        if (q == null) {
            return false;
        }
        delete(list, q);
        list.add(q);
        return true;
    }

    private static boolean delete(ArrayList<String[]> list, String[] q) {
        if (q == null) {
            return false;
        }
        if (q.length != list.get(0).length) {
            return false;
        }
        // update
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)[0].equals(q[0])) {
                list.remove(i);
            }
        }
        return true;
    }
}
